﻿using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;

namespace PageOffice6_Net_Core.Controllers.FormToDataRegions
{
    public class FormToDataRegionsController : Controller
    {
        private readonly IWebHostEnvironment _webHostEnvironment;
        public FormToDataRegionsController(IWebHostEnvironment webHostEnvironment)
        {
            _webHostEnvironment = webHostEnvironment;
        }
        public IActionResult Word()
        {
            PageOfficeNetCore.PageOfficeCtrl poCtrl = new PageOfficeNetCore.PageOfficeCtrl(Request);
            
            PageOfficeNetCore.Word.WordDocumentWriter wordDoc = new PageOfficeNetCore.Word.WordDocumentWriter();
            wordDoc.OpenDataRegion("PO_Buyer").Editing = true;
            wordDoc.OpenDataRegion("PO_Supplier").Editing = true;
            wordDoc.OpenDataRegion("PO_code").Editing = true;
            wordDoc.OpenDataRegion("PO_company").Editing = true;
            wordDoc.OpenDataRegion("PO_table").Editing = true;
            wordDoc.OpenDataRegion("PO_rmb").Editing = true;

            poCtrl.SetWriter(wordDoc);
            poCtrl.WebOpen("doc/test.docx", PageOfficeNetCore.OpenModeType.docSubmitForm, "tom");
            ViewBag.poCtrl = poCtrl.GetHtml();
            return View();
        }


        public async Task<ActionResult> SaveDoc()
        {
            PageOfficeNetCore.FileSaver fs = new PageOfficeNetCore.FileSaver(Request, Response);
            await fs.LoadAsync();
            string webRootPath = _webHostEnvironment.WebRootPath;
            fs.SaveToFile(webRootPath + "/FormToDataRegions/doc/" + fs.FileName);
            return   fs.Close();
            
        }
    }
}